<?php
/**
 * oscshop2 B2C电子商务系统
 *
 * ==========================================================================
 * @link      http://www.oscshop.cn/
 * @copyright Copyright (c) 2015-2016 oscshop.cn. 
 * @license   http://www.oscshop.cn/license.html License
 * ==========================================================================
 *
 * @author    李梓钿
 *
 */
 
namespace osc\member\controller;
use osc\common\controller\AdminBase;
use think\Db;
use think\Request;
use think\Session;
class MemberBackend extends AdminBase{
	
	protected function _initialize(){
		parent::_initialize();
		$this->assign('breadcrumb1','会员');
		$this->assign('breadcrumb2','会员管理');
		$this->assign('breadcrumb3','交易所账号管理');
	}
	
     public function index(){
		 $param=input('param.');
		 $user_state = input('param.user_state');
		 $query=[];
		if(isset($param['user_name'])){
			$map['m.username|m.nickname']=['like',"%".$param['user_name']."%"];
			$query['m.username']=urlencode($param['user_name']);
		}else{
			$map['m.uid']=['gt',0];
		}

		if(isset($param['sharemobile'])){		
			$map['sharemobile']=['like',"%".$param['sharemobile']."%"];
			//$query['m.sharemobile']=urlencode($param['sharemobile']);
		}
		if(isset($param['idcardname'])){		
			$map['idcardname']=['like',"%".$param['idcardname']."%"];
			//$query['m.sharemobile']=urlencode($param['sharemobile']);
		}
		if(isset($param['nickname'])){		
			$map['nickname']=['like',"%".$param['nickname']."%"];
			//$query['m.sharemobile']=urlencode($param['sharemobile']);
		}
		if(isset($param['begin_time']) || isset($param['end_time'])){
			 $map['regdate'] = [['>',strtotime($param['begin_time'])],['<',strtotime($param['end_time'])]];
		}

		 if($user_state == 1){	//查询游客
			 $map['user_state'] = 0;
		 }elseif($user_state == 2){ 	//查询会员
			 $map['user_state'] = 1;
		 }elseif($user_state == 3){ 	//查询会员
			 $map['user_state'] = ['>',1];
		 }
		$list=[];

		 $list=Db::name('member')->alias('m')->field('m.*')
		
		->where($map)->order('m.uid desc')->paginate(config('page_num'),false, [
						'query' => Request::instance()->param(),//不丢失已存在的url参数
				]);

		$this->assign('user_state',$user_state);

		$this->assign('list',$list);
				
		$this->assign('empty','<tr><td colspan="20">没有数据~</td></tr>');
		
    	return $this->fetch();
	 }

	 public function search(){     	

		$param=input('param.');
		//var_dump($param);
		
		$user = Db::name('member')->where('uid',$param['id'])->find();
		$sql="select * from osc_member where sharemobile = '".$user['username']."'";
		$sql1="select * from osc_member where sharemobile in(select username from osc_member where sharemobile = '".$user['username']."')"; 	
		//var_dump($map);

		$sql_count="select count(*) co from osc_member where sharemobile = '".$user['username']."'";
		$sql_count1="select count(*) co from osc_member where sharemobile in(select username from osc_member where sharemobile = '".$user['username']."')";
		
		$sql1_count="select count(*) co from osc_member where sharemobile = '".$user['username']."' and user_state>0";
		$sql1_count1="select count(*) co from osc_member where sharemobile in(select username from osc_member where sharemobile = '".$user['username']."' )and user_state>0";
		$list=[];
		
		$list1 = Db::query($sql);
		$list2 = Db::query($sql1);		
		
		$count1 = Db::query($sql_count);
		$count2 = Db::query($sql_count1);

		$count1_vip = Db::query($sql1_count);
		$count2_vip = Db::query($sql1_count1);

		//$this->assign('list',$list);
		$this->assign('list1',$list1);
		$this->assign('list2',$list2);

		$this->assign('count1',$count1);
		$this->assign('count2',$count2);

		$this->assign('count1_vip',$count1_vip);
		$this->assign('count2_vip',$count2_vip);				
		$this->assign('empty','<tr><td colspan="20">没有数据~</td></tr>');
		$this->assign('breadcrumb2','会员推荐查看');
    	return $this->fetch();
	 }

	/**
	 * 交易所账号管理
	 */
	 public function trade_acc(){
		 $param = $this->request->param();

		 $map = null;
		 $trade_id = '';
		 if(isset($param['user_name'])){
			 $map['b.username'] = $param['user_name'];
		 }
		 if(isset($param['exchange_id'])){
			 $map['a.exchange_id'] = $param['exchange_id'];
		 }
		 if(isset($param['trade_id'])){
			 $map['a.trade_id'] = ['like','%'.$param['trade_id'].'%'];
			 $trade_id = $param['trade_id'];
		 }


		 $list = Db::name('exchange_trade_acc a,osc_member b')
				 ->where('a.username=b.username')
				 ->where($map)
				 ->field('b.uid,a.username,a.exchange_id,a.trade_id,a.sub_trade_id,b.idcardname,b.idcard')
				 ->paginate(10,false, [
						 'query' => Request::instance()->param(),//不丢失已存在的url参数
				 ]);
		 $page = $list->render();
		 $info = $list->all();
		 foreach($info as &$value){
			 $value['trade_id'] = str_replace($trade_id, '<font color=red>'.$trade_id.'</font>',$value['trade_id']);//替换 把搜索内容标红
		 }

		 $house = Db::name('exchange_house')->where('state',1)->select();
//		 echo'<pre>';print_r($list);
		 $this->assign('house',$house);
		 $this->assign('page',$page);
		 $this->assign('list',$info);
		 return $this->fetch();
	 }

	 public function add(){
	 	
		if(request()->isPost()){
			$date=input('post.');
			$result = $this->validate($date,'Member');			
			if($result!==true){
			
				return ['error'=>$result];
			}
			$member['username']=$date['username'];
			$member['password']=think_ucenter_encrypt($date['password'],config('PWD_KEY'));
			$member['regdate']=time();
			$member['reg_type']='pc';
			$member['email']=$date['email'];
			$member['telephone']=$date['telephone'];
			$member['groupid']=$date['groupid'];
			
			$uid=Db::name('member')->insert($member,false,true);
			
			if($uid){
				
				Db::name('member_auth_group_access')->insert(['uid'=>$uid,'group_id'=>$date['groupid']]);
				
				storage_user_action(UID,session('user_auth.username'),config('BACKEND_USER'),'新增了会员');
			
				return ['success'=>'新增成功','action'=>'add'];
			}else{
				return ['error'=>'新增失败'];
				
			}
			
		}
		$this->assign('group',Db::name('member_auth_group')->field('id,title')->select());
		$this->assign('crumbs','新增');
	 	return $this->fetch();
	 }

 	 public function edit(){
		
		if(request()->isPost()){
		
			$date=input('post.');			
			$member['password']=think_ucenter_encrypt($date['password'],config('PWD_KEY'));		
			$member['2password']=think_ucenter_encrypt($date['2password'],config('PWD_KEY'));		
			$member['email']=$date['email'];
			$member['checked']=$date['checked'];
			$member['telephone']=$date['telephone'];
			$member['groupid']=$date['groupid'];
			$member['sharemobile']=$date['sharemobile'];
			$member['predeposit']=$date['predeposit'];
			$member['idcardname']=$date['idcardname'];
			$member['idcard']=$date['idcard'];
			$member['bao']=$date['bao'];
			$member['user_state']=$date['user_state'];
			$member['jilt']=$date['jilt'];
			$member['jilin_bean']=$date['jilin_bean'];
			$member['id6']=$date['id6'];
			
			$m=Db::name('member')->find($date['uid']);
			
			$username=empty($m['username'])?$m['nickname']:$m['username'];
			
			//余额
			
			if($member['predeposit']>$m['predeposit']){
				$prefix=1;//增加
				$money=($member['predeposit']-$m['predeposit']);
				$m['predeposit'] = (int)$m['predeposit'];
				$member['predeposit'] =(int)$member['predeposit'];
				$description=' 金豆,'.'数量增加 '.$money.' 原金豆数量'.$m['predeposit'].' 当前金豆数量'.$member['predeposit'];
			}else{
				$prefix=2;//减少
				$money=($m['predeposit']-$member['predeposit']);
				$m['predeposit'] = (int)$m['predeposit'];
				$member['predeposit'] = (int)$member['predeposit'];
				$description='金豆,'.'数量减少 '.$money.' 原金豆数量'.$m['predeposit'].' 当前金豆数量'.$member['predeposit'];
			}
			
			
			if(Db::name('member')->where('uid',$date['uid'])->update($member)){				
				
				
				if($member['predeposit']!=$m['predeposit']){	
					
					Db::name('predeposit')->insert(array(
						'username'=>$username,
						'uid'=>$date['uid'],					
						'money'=>$money,
						'description'=>$description,
						'prefix'=>$prefix,
						'creat_time'=>time(),
						'pay_status'=>1,
						'admin_id'=>UID
					));
					storage_user_action(UID,session('user_auth.username'),config('BACKEND_USER'),$description);
				}
				
				
				Db::name('member_auth_group_access')->where('uid',$date['uid'])->update(['group_id'=>$date['groupid']]);
				
				storage_user_action(UID,session('user_auth.username'),config('BACKEND_USER'),'编辑了会员');
				$this->success('编辑成功',url('MemberBackend/index'));
			}else{
				$this->error('编辑失败');
			}
		}
		$list=[
			'info'=>Db::name('member')->find(input('param.id')),
			'address'=>Db::name('address')->where('uid',input('param.id'))->select()
		];
		$trade = Db::name('exchange_trade_acc')->where('username',$list['info']['username'])->select();
		$this->assign('data',$list);
		$this->assign('trade',$trade);
		$this->assign('group',Db::name('member_auth_group')->field('id,title')->select());
		$this->assign('crumbs','会员资料');
	 	return $this->fetch('info');
	 }

	/**
	 * 修改老用户直接拍单状态
	 * @throws \think\Exception
	 */
	public function update_user_rang_num(){
		$data = $this->request->param();
		if($data){
			$res = Db::name('member')->where('username',$data['username'])->update(['user_rang_num'=>$data['status']]);
			if(!Db::name('member_rang_num')->where('username',$data['username'])->count()){
				$red = Db::name('member_rang_num')->insert(['username'=>$data['username'],'state'=>$data['status']]);
			}else{
				$red = Db::name('member_rang_num')->where('username',$data['username'])->update(['state'=>$data['status']]);
			}
			if($res && $red){
				$this->redirect('member_backend/user_rang_num');
			}else{
				$this->error('修改失败');
			}
		}else{
			$this->error('数据错误');
		}
	}


	public function user_rang_num(){
		$param=input('param.');
		$user_state = input('param.user_state');
		if(isset($param['user_name'])){
			$map['m.username|m.nickname']=['like',"%".$param['user_name']."%"];
		}else{
			$map['m.uid']=['gt',0];
		}
		if(isset($param['idcardname'])){
			$map['idcardname']=['like',"%".$param['idcardname']."%"];
		}
		if(isset($param['nickname'])){
			$map['nickname']=['like',"%".$param['nickname']."%"];
		}

		if($user_state == 1){	//查询游客
			$map['user_state'] = 0;
		}elseif($user_state == 2){ 	//查询会员
			$map['user_state'] = ['>=',1];
		}
		$list=[];

		$list=Db::name('member')->alias('m')->field('m.*')

				->where($map)->order('m.uid desc')->paginate(config('page_num'),false, [
						'query' => Request::instance()->param(),//不丢失已存在的url参数
				]);

		$this->assign('user_state',$user_state);

		$this->assign('list',$list);
		$this->assign('breadcrumb1','会员');
		$this->assign('breadcrumb2','允许老会员拍单');
		$this->assign('empty','<tr><td colspan="20">没有数据~</td></tr>');

		return $this->fetch();
	}


	/**
	 * 设置用户为黑名单
	 * @throws \think\Exception
	 */
	public function blacklist(){
		$data = $this->request->param();
		$res = Db::name('member')->where('uid',$data['id'])->update(['blacklist'=>$data['status']]);
		if($res){
			$this->redirect('member_backend/index');
		}else{
			$this->error('设置失败','member_backend/index');
		}
	}

	/**
	 * 后台登陆前台用跳过二级密码验证
	 */
	function page_logo(){
		$data = $this->request->param();
		$user = Db::name('member')->where('username',$data['username'])->find();
		if($user){

			$auth = array(
					'uid'             => $user['uid'],
					'username'        => $user['username'],
					'nickname'        => $user['nickname'],
					'group_id'		  => $user['groupid'],
			);
			osc_service('common','member')->refresh_member($auth);

			Session::set('username',$user['username']);
			Session::set('warn','warn');
			Session::set('passwd2','passwd2');
			Session::set('mobile_total',osc_cart()->count_cart_total($user['uid']));

			$this->redirect('mobile/user/index');
		}else{
			$this->error('跳转失败');
		}
	}
 
}
?>